<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>用户登录</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: Arial, sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      margin: 0;
      background-image: -webkit-linear-gradient(135deg, #f0f2f5 0%, #e8eaf6 100%);
      background-image: -moz-linear-gradient(135deg, #f0f2f5 0%, #e8eaf6 100%);
      background-image: -o-linear-gradient(135deg, #f0f2f5 0%, #e8eaf6 100%);
      background-image: linear-gradient(135deg, #f0f2f5 0%, #e8eaf6 100%);
    }

    .login-container {
      background-color: white;
      padding: 2rem;
      border-radius: 10px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1), 0 0 1px rgba(0, 0, 0, 0.1);
      animation: fadeInUp 0.5s ease-out;
      transition: all 0.3s ease;
    }

    @keyframes fadeInUp {
      from {
        opacity: 0;
        transform: translateY(10px);
      }

      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    .login-container:hover {
      box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15), 0 0 1px rgba(0, 0, 0, 0.1);
    }

    h2 {
      text-align: center;
      color: #1a73e8;
      margin-bottom: 1.5rem;
      font-size: 2rem;
      letter-spacing: 0.5px;
      text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    }

    .form-group {
      margin-bottom: 1.2rem;
      position: relative;
    }

    label {
      display: block;
      margin-bottom: 0.5rem;
      color: #333;
      font-weight: 500;
      transition: color 0.3s ease;
    }

    input {
      width: 100%;
      padding: 0.8rem;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-sizing: border-box;
      transition: border-color 0.3s ease, box-shadow 0.3s ease;
    }

    input:focus {
      outline: none;
      border-color: #1a73e8;
      box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
    }

    button {
      width: 100%;
      padding: 1rem;
      background-color: #1a73e8;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 1rem;
      transition: background-color 0.3s, transform 0.3s ease;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    button:hover {
      background-color: #1557b0;
      transform: translateY(-2px);
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
    }

    button:active {
      transform: translateY(1px);
    }

    /* 提示信息样式，使用 visibility: hidden 替代 display: none */
    .form-group.error-message {
      color: #f44336;
      font-size: 0.9rem;
      margin-top: 0.3rem;
      visibility: hidden;
      /* 初始隐藏，占据空间 */
      opacity: 0;
      /* 透明度为0，视觉上不可见 */
      transition: opacity 0.3s ease, visibility 0.3s ease;
      /* 添加过渡效果 */
    }
  </style>
</head>

<body>
  <div class="login-container">
    <h2>用户登录</h2>
    <form id="loginForm">
      <div class="form-group">
        <label for="username">用户名：</label>
        <input type="text" id="username" required>
        <span class="error-message" id="usernameError">用户名不能为空</span>
      </div>
      <div class="form-group">
        <label for="password">密码：</label>
        <input type="password" id="password" required>
        <span class="error-message" id="passwordError">密码不能为空</span>
      </div>
      <button type="submit">登录</button>
    </form>
  </div>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    const loginForm = document.getElementById('loginForm');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    const usernameError = document.getElementById('usernameError');
    const passwordError = document.getElementById('passwordError');

    usernameInput.addEventListener('input', function () {
      if (this.value.trim() === '') {
        usernameError.style.opacity = 1; // 显示错误信息
        usernameError.style.visibility = 'visible';
      } else {
        usernameError.style.opacity = 0; // 隐藏错误信息
        usernameError.style.visibility = 'hidden';
      }
    });

    passwordInput.addEventListener('input', function () {
      if (this.value.trim() === '') {
        passwordError.style.opacity = 1; // 显示错误信息
        passwordError.style.visibility = 'visible';
      } else {
        passwordError.style.opacity = 0; // 隐藏错误信息
        passwordError.style.visibility = 'hidden';
      }
    });

    loginForm.addEventListener('submit', function (e) {
      e.preventDefault();
      const username = usernameInput.value.trim();
      const password = passwordInput.value.trim();

      if (username === '') {
        usernameError.style.opacity = 1;
        usernameError.style.visibility = 'visible';
      } else {
        usernameError.style.opacity = 0;
        usernameError.style.visibility = 'hidden';
      }

      if (password === '') {
        passwordError.style.opacity = 1;
        passwordError.style.visibility = 'visible';
      } else {
        passwordError.style.opacity = 0;
        passwordError.style.visibility = 'hidden';
      }

      if (username === '' || password === '') {
        return;
      }

      const formData = new URLSearchParams();
      formData.append('username', username);
      formData.append('password', password);

      axios({
        method: 'post',
        url: 'https://dhrdfawueimg.sealoshzh.site/api/login',
        data: {
          username: username,
          password: password
        }
      }).then(function (response) {
        alert('登录成功');

        const token = response.data.token;
        sessionStorage.setItem('token', token);
        alert(token);
        window.location.href = '../qianduan(1)/frontend/index.html' // 将 token 存储在 sessionStorage 中
      }).catch(function (error) {
        alert(error.response.data.message);
      })


    });
  </script>
</body>

</html>